package com.kld.simplelauncher.app;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kld.simplelauncher.R;
import com.kld.simplelauncher.databinding.MoreAppLayoutBinding;
import com.kld.simplelauncher.utils.AppUtils;
import com.kld.simplelauncher.utils.EntryActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MoreAppActivity extends AppCompatActivity  {
    private static final String TAG = "MoreAppActivity";
    private MoreAppLayoutBinding binding;
    private List<EntryActivity> infoList;
    private MoreAppPageAdapter adapter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = MoreAppLayoutBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        infoList = AppUtils.getAllInstalledAppEntries(this);
        adapter = new MoreAppPageAdapter(getSupportFragmentManager(),infoList);
        binding.allAppPage.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_PACKAGE_ADDED);
        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
        filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
        filter.addDataScheme("package");  // 只监听包名相关的事件
        registerReceiver(appReceiver, filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(appReceiver);
    }

    private static final int UPDATE_UI = 0x001;
    private Handler mHandler = new Handler(Objects.requireNonNull(Looper.myLooper())) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case UPDATE_UI:
//                    infoList = AppUtils.getAllInstalledAppEntries(MoreAppActivity.this);
//                    adapter.setAllAppList(infoList);
//                    binding.allAppPage.setAdapter(adapter);
                    finish();
                    break;
            }
        }
    };

    private BroadcastReceiver appReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {
                // 应用安装
                String packageName = intent.getData().getSchemeSpecificPart();
                Log.d(TAG, "onReceive: 应用安装 = " + packageName);
                mHandler.sendEmptyMessageDelayed(UPDATE_UI,1000);
            } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {
                // 应用卸载
                String packageName = intent.getData().getSchemeSpecificPart();
                Log.d(TAG, "onReceive: 应用卸载 = " + packageName);
                mHandler.sendEmptyMessageDelayed(UPDATE_UI,1000);
            } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {
                // 应用更新
                String packageName = intent.getData().getSchemeSpecificPart();
                Log.d(TAG, "onReceive: 应用更新 = " + packageName);
                mHandler.sendEmptyMessageDelayed(UPDATE_UI,1000);
            }
        }
    };
}
